package com.sczmgk.hotline_pzh.alipay;

/**
 * 支付结果
 *
 */
public class PayResult {
	
	
	
	/**
	 * 状态码
	 */
	private String resultStatus;
	/**
	 * 结果字符串
	 */
	private String result;
	/**
	 * 备注状态
	 */
	private String memo;
	/**
	 * 商户ID
	 */
	private String partner;
	/**
	 * 卖家支付用户号
	 */
	private String seller_id;
	/**
	 * 商户网站唯一订单号
	 */
	private String out_trade_no;
	/**
	 * 商品名称
	 */
	private String subject;
	/**
	 * 商品描述
	 */
	private String body;
	/**
	 * 交易金额
	 */
	private String total_fee;
	/**
	 * 通知url
	 */
	private String notify_url;
	/**
	 * 支付成功的状态
	 */
	private String success;
	/**
	 * 返回的URL
	 */
	private String return_url;
	/**
	 * 签名文件
	 */
	private String sign;
	
	/**
	 * 构造方法
	 * @param rawResult
	 */
	public PayResult(String rawResult) {

		if (isEmpty(rawResult))
			return;

		String[] resultParams = rawResult.split(";");// 以;分隔符分开
		for (String resultParam : resultParams) {
			if (resultParam.startsWith("resultStatus")) {
				resultStatus = gatValue(resultParam, "resultStatus");
			}
			if (resultParam.startsWith("result")) {
				result = gatValue(resultParam, "result");
			}
			if (resultParam.startsWith("memo")) {
				memo = gatValue(resultParam, "memo");
			}
		}
		String[] resultDetails = result.split("&");
		for (String resultDetail:resultDetails) {//取得里面的参数
		
			if(resultDetail.startsWith("partner")){
				partner = getDetailValue(resultDetail, "partner");
			}
			if(resultDetail.startsWith("seller_id")){
				seller_id = getDetailValue(resultDetail, "seller_id");
			}
			if(resultDetail.startsWith("out_trade_no")){
				out_trade_no = getDetailValue(resultDetail, "out_trade_no");
			}
			if(resultDetail.startsWith("subject")){
				subject = getDetailValue(resultDetail, "subject");
			}
			if(resultDetail.startsWith("body")){
				body = getDetailValue(resultDetail, "body");
			}
			if(resultDetail.startsWith("total_fee")){
				total_fee = getDetailValue(resultDetail, "total_fee");
			}
			if(resultDetail.startsWith("notify_url")){
				notify_url = getDetailValue(resultDetail, "notify_url");
			}
			if(resultDetail.startsWith("success")){
				success = getDetailValue(resultDetail, "success");
			}
			if(resultDetail.startsWith("return_url")){
				return_url = getDetailValue(resultDetail, "return_url");
			}
			if(resultDetail.startsWith("sign=")){
				sign = getDetailValue(resultDetail, "sign");
			}
		}
	}
	
	/**
	 *取得商户id
	 * @return
	 */
	public String getPartner() {
		return partner;
	}

	/**
	 * 卖家支付用户号
	 * @return
	 */
	public String getSeller_id() {
		return seller_id;
	}

	/**
	 * 商户网站唯一订单号
	 * @return
	 */
	public String getOut_trade_no() {
		return out_trade_no;
	}
	/**
	 * 商品名称
	 * @return
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * 商品描述
	 * @return
	 */
	public String getBody() {
		return body;
	}

	/**
	 * 交易金额
	 * @return
	 */
	public String getTotal_fee() {
		return total_fee;
	}

	/**
	 * 通知URL
	 * @return
	 */
	public String getNotify_url() {
		return notify_url;
	}

	/**
	 * 成功状态
	 * @return
	 */
	public String getSuccess() {
		return success;
	}

	/**
	 * 返回的URL
	 * @return
	 */
	public String getReturn_url() {
		return return_url;
	}

	/**
	 * 签名
	 * @return
	 */
	public String getSign() {
		return sign;
	}

	/***
	 * 取得未签名的字符串
	 * @return
	 */
	public String getSignRaw(){
		int index =  result.indexOf("&sign_type=");
		return result.substring(0, index);
	}
	/**
	 *   取得result里的详情字段
	 * 
	 * @param content
	 * @param key
	 * @return
	 */
	private String getDetailValue(String content, String key){  //   \"0.01\"
		String result = "";
		String temp = "";
	    temp =content.substring(content.indexOf("=")+1 );//取得key =  后的字符串
	    result = temp.substring(1, temp.length()-1);
        return result;
	}
	/**
	 * 取得值
	 * @param content    如：resultStatus={9000}
	 * @param key　　　　resultStatus
	 * @return　　9000
	 */
	private String gatValue(String content, String key) {
		String prefix = key + "={";//拼装key = { 
		return content.substring(content.indexOf(prefix) + prefix.length(),content.lastIndexOf("}"));//取得key = { 后的字符串
	}

	/**
	 * @return the resultStatus
	 */
	public String getResultStatus() {
		return resultStatus;
	}

	/**
	 * @return the memo
	 */
	public String getMemo() {
		return memo;
	}

	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	@Override
	public String toString() {
		return "PayResult [resultStatus=" + resultStatus + ", result=" + result
				+ ", memo=" + memo + ", partner=" + partner + ", seller_id="
				+ seller_id + ", out_trade_no=" + out_trade_no + ", subject="
				+ subject + ", body=" + body + ", total_fee=" + total_fee
				+ ", notify_url=" + notify_url + ", success=" + success
				+ ", return_url=" + return_url + ", sign=" + sign + "]";
	}
	 /**
     * 是否为空
     * 
     * @param value
     * @return
     */
    public static boolean isEmpty(String value) {
        if (value == null || value.length() == 0 || "null".equals(value)) {
            return true;
        } else
            return false;
    }
}
